﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Laugh.Data;
using Laugh.Example.Domain;

namespace Laugh.Example.Services
{
    public class DefaultReportService : IReportService
    {
        private IRepository<Person> _repository;
        private IRepository<Function> _repository2;

        public DefaultReportService(IRepository<Person> repository, IRepository<Function> repository2)
        {
            _repository = repository;
            _repository2 = repository2;
        }

        #region IReportService 成员

        public string Name()
        {
            Log.Info("return person's name.");
            return _repository.All.First().Name;
        }

        public int Age()
        {
            Log.Info("return person's age.");
            return _repository.All.First().Age;
        }


        public Person Get(int id)
        {
            return _repository.Get(i => i.Id == id && i.Age == 2009);
        }

        public void Update(Person p)
        {
            _repository.SaveOrUpdate(p);
        }

        public IEnumerable<Function> UserPrivilege(int id)
        {
            return _repository2.Fetch(pri => pri.PersonId == id);
        }

        public Dictionary<Person, IEnumerable<Person>> UserPerson(int id)
        {
            Dictionary<Person, IEnumerable<Person>> af = new Dictionary<Person, IEnumerable<Person>>();
            var priList = UserPrivilege(1);
            var fids = priList.Select(p => p.Id);
            foreach (var p in priList)
            {
                Person f = Get(p.PersonId);
                if (f == null) continue;

                af.Add(f, null);
            };

            return af;
        }
        #endregion
    }
}